<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Company extends Eloquent {

	use SoftDeletingTrait;
	
	protected $dates = ['deleted_at'];
	
	protected $table = 'company';

	/**
	 * Condition: Unarchived Items.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @param $query
	 * @return mixed
	 */
	public function scopeActive( $query ) {
		return $query->where($this->table . '.archived', 0);
	}

	/**
	 * Condition: Unarchived Order Items and with given status.
	 * @author AngelaMisa 02/23/2015 9:51PM
	 * @param $query, $status
	 * @return mixed
	 */
	public function scopeHasOrders( $query, $status = null ) {
		return $query->whereHas('companyOrders', function( $q ) use ( $status ) {
			$q->whereHas('orderProducts', function ($query) use ($status) {
				$query->where('order_product.archived', 0);
				if ( !is_null($status) ) $query->where( 'order_product.status', $status );
			});
		});
	}

	/**
	 * Creates one to many relationship of Company to CompanyBranch.
	 * @return mixed
	 */
	public function companyBranch() {
		return $this->hasMany('CompanyBranch', 'COMPANY_id');
	}

	/**
	 * Retrieves Orders under Company.
	 * @author AngelaMisa 02/22/2015 2:25AM
	 * @return mixed
	 */
	public function companyOrders() {
		return $this->hasMany('Order', 'COMPANY_id');
	}
}